package com.kalvan.core.security;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Scanner;

/**
 * @author chenliang
 */
public class DesUtil {
    public static final String DEFAULT_KEY = "kalvan88";
    private static final String CHARSET = "UTF-8";

    /**
     * "DES" "DES/ECB/PKCS5Padding cryptMode取值
     * 加密Cipher.ENCRYPT_MODE,解密Cipher.DECRYPT_MODE
     *
     * @param factory
     * @param cipherName
     * @param cryptMode
     * @param iv
     * @return
     * @throws Exception
     */
    static Cipher getCipher(String factory, String cipherName,
                            int cryptMode, byte[] iv, byte[] m_desKey) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 一个SecretKey对象
        SecretKey key = new SecretKeySpec(m_desKey, factory);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(cipherName);
        // 用密匙初始化Cipher对象
        if (iv != null) {
            IvParameterSpec ips = new IvParameterSpec(iv);
            cipher.init(cryptMode, key, ips, sr);
        } else {
            cipher.init(cryptMode, key, sr);
        }
        return cipher;

    }

    /**
     * 加密扩展数据
     *
     * @param param
     * @return
     * @throws Exception
     */
    public static String encryptExtraData(String param) throws Exception {
        return encryptExtraData(DEFAULT_KEY, param);
    }

    /**
     * 加密扩展数据
     *
     * @param param
     * @return
     * @throws Exception
     */
    public static String encryptExtraData(String desKey, String param) throws Exception {
        if (StringUtils.isBlank(param)) {
            return param;
        }
        Cipher cipher = getCipher("DES", "DES/ECB/PKCS5Padding",
                Cipher.ENCRYPT_MODE, null, desKey.getBytes(CHARSET));
        byte[] plainText = param.getBytes(CHARSET);
        byte[] encryptedData = cipher.doFinal(plainText, 0, plainText.length);
        return Base64.encodeBase64String(encryptedData);
    }

    /**
     * 解密扩展数据
     *
     * @param param
     * @return
     * @throws Exception
     */
    public static String decryptExtraData(String desKey, String param) throws Exception {
        if (StringUtils.isBlank(param)) {
            return param;
        }
        Cipher cipher = getCipher("DES", "DES/ECB/PKCS5Padding",
                Cipher.DECRYPT_MODE, null, desKey.getBytes(CHARSET));
        byte[] encryptByte = Base64.decodeBase64(param);
        byte[] decryptedData = cipher.doFinal(encryptByte, 0, encryptByte.length);
        return new String(decryptedData, CHARSET);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(decryptExtraData(DEFAULT_KEY, "LHuUUecy9Cs="));
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入明文：");
        String param = scan.next();
        System.out.println("生成信息");
        System.out.println("加密码密码: " + DesUtil.encryptExtraData(DesUtil.DEFAULT_KEY, param));
        main(args);
    }
}
